import 'package:backend.core/core/domain/entities/api/app_store/api_app_store_one_time_purchased_item_transaction.dart';
import 'package:backend.core/core/domain/entities/api/app_store/api_app_store_transaction_revocation_info.dart';
import 'package:backend.core/core/domain/entities/app_store/app_store_one_time_purchased_item_transaction.dart';
import 'package:backend.purchases/domain/helpers/api/app_store/create_api_app_store_offer_type_from_internal_one.dart';
import 'package:backend.purchases/domain/helpers/api/app_store/create_api_app_store_ownership_type_from_internal_one.dart';
import 'package:backend.purchases/domain/helpers/api/app_store/create_api_app_store_revocation_info_from_internal_one.dart';

abstract class CreateApiAppStoreOneTimePurchasedItemTransactionFromInternalOne {
  ApiAppStoreOneTimePurchasedItemTransaction call({
    required AppStoreOneTimePurchasedItemTransaction oneTimePurchasedItemTransaction,
  });
}

class CreateApiAppStoreOneTimePurchasedItemTransactionFromInternalOneImpl
    implements CreateApiAppStoreOneTimePurchasedItemTransactionFromInternalOne {
  const CreateApiAppStoreOneTimePurchasedItemTransactionFromInternalOneImpl({
    required CreateApiAppStoreTransactionOfferTypeFromInternalOne
        createApiAppStoreTransactionOfferTypeFromInternalOne,
    required CreateApiAppStoreTransactionOwnershipTypeFromInternalOne
        createApiAppStoreTransactionOwnershipTypeFromInternalOne,
    required CreateApiAppStoreTransactionRevocationInfoFromInternalOne
        createApiAppStoreTransactionRevocationInfoFromInternalOne,
  })  : _createApiAppStoreTransactionOfferTypeFromInternalOne =
            createApiAppStoreTransactionOfferTypeFromInternalOne,
        _createApiAppStoreTransactionOwnershipTypeFromInternalOne =
            createApiAppStoreTransactionOwnershipTypeFromInternalOne,
        _createApiAppStoreTransactionRevocationInfoFromInternalOne =
            createApiAppStoreTransactionRevocationInfoFromInternalOne;

  final CreateApiAppStoreTransactionOfferTypeFromInternalOne
      _createApiAppStoreTransactionOfferTypeFromInternalOne;
  final CreateApiAppStoreTransactionOwnershipTypeFromInternalOne
      _createApiAppStoreTransactionOwnershipTypeFromInternalOne;
  final CreateApiAppStoreTransactionRevocationInfoFromInternalOne
      _createApiAppStoreTransactionRevocationInfoFromInternalOne;

  @override
  ApiAppStoreOneTimePurchasedItemTransaction call({
    required AppStoreOneTimePurchasedItemTransaction oneTimePurchasedItemTransaction,
  }) {
    final apiOfferType = _createApiAppStoreTransactionOfferTypeFromInternalOne(
      offerType: oneTimePurchasedItemTransaction.offerType,
    );

    final apiOwnershipType = _createApiAppStoreTransactionOwnershipTypeFromInternalOne(
      ownershipType: oneTimePurchasedItemTransaction.ownershipType,
    );

    ApiAppStoreTransactionRevocationInfo? apiRevocationInfo;

    if (oneTimePurchasedItemTransaction.revocationInfo != null) {
      apiRevocationInfo = _createApiAppStoreTransactionRevocationInfoFromInternalOne(
        revocationInfo: oneTimePurchasedItemTransaction.revocationInfo!,
      );
    }

    return ApiAppStoreOneTimePurchasedItemTransaction(
      id: oneTimePurchasedItemTransaction.id,
      time: oneTimePurchasedItemTransaction.time,
      offerType: apiOfferType,
      ownershipType: apiOwnershipType,
      revocationInfo: apiRevocationInfo,
    );
  }
}
